﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;

using GpsTagger.Interface;
using GpsTagger.Logic;

namespace GpsTagger.Interface
{
    public partial class taggerFrm : Form
    {
        ListBox.ObjectCollection filesToProcess;
        string copyrightString;
        PictureTagger pt;

        public taggerFrm(ListBox.ObjectCollection files, string copyright)
        {
            InitializeComponent();
            filesToProcess = files;
            copyrightString = copyright;
        }

        private void okayButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        void Process()
        {
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            selectionStart = 0;
            errors = 0;
            progressBar1.Maximum = filesToProcess.Count;
            for (int i = 0; i < filesToProcess.Count; i++)
            {                
                ProcessFile(filesToProcess[i]);
                progressBar1.Value = i + 1;
            }
            if (errors > 0)
            {
                string textToOutput = Environment.NewLine+"There were errors. Please review them!";
                outputText.AppendText(textToOutput);
                outputText.Select(selectionStart, selectionStart + textToOutput.Length);
                outputText.SelectionColor = Color.Red;
                outputText.SelectionFont = new Font(outputText.Font, FontStyle.Bold);
                outputText.Select(outputText.Text.Length, 0);
            }
            okayButton.Visible = true;
        }

        int selectionStart = 0;
        int errors = 0;

        void ProcessFile(object obj)
        {
            ImageInfo nfo = (ImageInfo)obj;
            if (nfo != null)
            {
                if (nfo.Mp.Name != "[NOT TAGGED]")
                {
                    int i = 4; // 4 retires
                    bool result = false;
                    while((i>0) && (!result))
                    {
                        result = pt.TagPicture(nfo.FileName, nfo.Mp, copyrightString);
                        if (!result)
                        {
                            Thread.Sleep(100);
                        }
                        i--;
                    }
                    if (result)
                    {
                        string textToOutput = nfo.FileName + " tagging succeded" + Environment.NewLine;
                        outputText.AppendText(textToOutput);
                        outputText.Select(selectionStart, selectionStart + textToOutput.Length);
                        outputText.SelectionColor = Color.Green;
                        selectionStart = outputText.Text.Length; 
                        outputText.Select(outputText.Text.Length, 0);
                    }
                    else
                    {
                        string textToOutput = nfo.FileName + " tagging FAILED !!" + Environment.NewLine + "Details: " + pt.GetLastOutput() + Environment.NewLine;
                        outputText.AppendText(textToOutput);
                        outputText.Select(selectionStart, selectionStart + textToOutput.Length);
                        outputText.SelectionColor = Color.Red;
                        selectionStart = outputText.Text.Length;
                        outputText.Select(outputText.Text.Length, 0);
                        errors++;
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            Process();
        }

        private void taggerFrm_Load(object sender, EventArgs e)
        {
            pt = new PictureTagger(Application.StartupPath + "\\exiftool.exe");
            timer1.Enabled = true;
        }
    }
}
